<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // var minimumRemoval = function (beans) {
    //   const sum = beans.reduce((cur, next) => cur + next, 0)
    //   let ans = sum
    //   beans.sort((a, b) => a - b)
    //   for (let i = 0; i < beans.length; i++) {
    //     ans = Math.min(ans, sum - beans[i] * (beans.length - i))
    //   }
    //   return ans
    // };
    var minimumRemoval = function (beans) {
      beans = beans.sort((a, b) => { return a - b; });
      let sum = 0, ans = 0;
      for (let i = 0; i < beans.length; i++) {
        sum = Math.max(sum, beans[i] * (beans.length - i))
      }
      for (let i = 0; i < beans.length; i++) {
        ans += beans[i];
      }
      return ans - sum;
    };
    console.log(minimumRemoval([4, 1, 6, 5]));
  </script>
</body>

</html>